package com.liangzhong.model.leagueMatch.vo
{
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.MatchType;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;

	/**
	 * 冠军赛信息vo
	 * @author lake
	 * 
	 */	
	public class ChampionMatchInfoVo extends EventDispatcher
	{
		/**
		 *打到的比赛index 未打过
		 * 1 第一轮
		 * 2 第二轮
		 * 3 第三轮
		 * 4 第四轮
		 * 5 第四轮
		 */		
		public var matchindex		:int;
		/**
		 *自己打到的比赛index
		 * 1 第一轮
		 * 2 第二轮
		 * 3 第三轮
		 * 4 第四轮
		 * 5 第四轮
		 */		
		public var selfIndex		:int;
		/**
		 *获胜场次
		 */		
		public var win				:int;
		/**
		 * 比赛结果信息
		 * {hcid:1001,gcid:1002,hgoal:1,ggoal:2}
		 * 主队id 客队id 主场进球数  客场进球数
		 */		
		public var matchResList	:Array = [];
		
		public function ChampionMatchInfoVo()
		{
			
		}
		/**
		 *解析 
		 * @param data
		 * 
		 */		
		public function parse(data:Object):void
		{
			matchResList = [];
			win = data.win;
			var selfId :int = AppData.getInstance().user.uid;
			var obj :Object ;
			//var rightParr :Array = [5,6,7,8,11,12,14];
			for(var i:int = 1;i <= 5;i++)
			{
				obj = {};
				obj.hcid  = int(data["hcid" + i]);
				obj.gcid  = int(data["gcid" + i]);
				obj.hgoal = int(data["hgoal" + i]);
				obj.ggoal = int(data["ggoal" + i]);
				obj.selfIndex = 0;
				/*if(obj.hcid == selfId)
				{
					obj.pos = 0;//上下位置 0 上 1 下
				}
				else
				{
					obj.pos = 1;
				}*/
				//左右位置 0 左 1 右
				/*if(rightParr.indexOf(i) != -1)
				{
					obj.wpos = 1;
				}
				else
				{
					obj.wpos = 0;
				}*/
				//test
				/*if(i <= 12)
				{
					obj.hcid = 10000 + i;
					obj.gcid = 10020 + i;
				}
				if(i <= 8)
				{
					obj.hgoal = int(Math.random() * 10);
					obj.ggoal = int(Math.random() * 10);
				}
				if(i == 1)
				{
					obj.hcid = AppData.getInstance().user.uid;
				}
				if(i == 9)
				{
					obj.hcid = AppData.getInstance().user.uid;
				}*/
				matchResList.push(obj);
			}
			//setType();
			matchindex = win + 1;
			if(matchindex > 5)
			{
				matchindex = 5;
			}
			this.dispatchEvent(new Event(Event.CHANGE));
			if(win == 5)//拿到冠军
			{
				AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MATCH_CHAMPION_SUCCESS);
				AppData.getInstance().leagueMatchManager.ntype = MatchType.LEGEND_MATCH;
			}
		}
		/**
		 *设置第几轮 
		 * 
		 */		
		private function setType():void
		{
			/*if(matchResList[14].hcid != 0 )
			{
				matchindex = 4;
			}
			else if(matchResList[13].hcid != 0)
			{
				matchindex = 3;
			}
			else if(matchResList[9].hcid != 0)
			{
				matchindex = 2;
			}
			else if(matchResList[0].hcid != 0)
			{
				matchindex = 1;
			}
			var selfId :int = AppData.getInstance().user.uid;
			var tempIndex :int;
			for(var i:int = 14;i >= 0;i--)
			{
				if(matchResList[i].hcid == selfId || matchResList[i].gcid == selfId)
				{
					if(i == 14)
					{
						tempIndex = 4;
					}
					else if(i == 13 || i == 12)
					{
						tempIndex = 3;
					}
					else if(i>=8 && i<=11)
					{
						tempIndex = 2;
					}
					else if(i>=0 && i<=7)
					{
						tempIndex = 1;
					}
					if(tempIndex >= selfIndex)
					{
						selfIndex = tempIndex;
					}
					matchResList[i].selfIndex = tempIndex;
				}
			}*/
			if(matchResList[4].gcid != 0 )
			{
				matchindex = 5;
			}
			else if(matchResList[3].gcid != 0 )
			{
				matchindex = 4;
			}
			else if(matchResList[2].gcid != 0)
			{
				matchindex = 3;
			}
			else if(matchResList[1].gcid != 0)
			{
				matchindex = 2;
			}
			else if(matchResList[0].gcid != 0)
			{
				matchindex = 1;
			}
		}
		/**
		 *更新比赛结果 
		 * @param data
		 * 
		 */		
		public function updateResult(data :Object):void
		{
			this.dispatchEvent(new Event(Event.CHANGE));
		}
	}
}